package com.king.freefish.online.controller;

import com.king.freefish.common.util.R;
import com.king.freefish.online.service.OnlineUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @packageName com.king.freefish.online.controller
 * @author: jinguoguang
 * @date: 2025/7/29 15:36
 * @description: 在线用户管理
 **/
@RestController
@RequestMapping("/api/online")
@AllArgsConstructor
public class OnlineUserController {


    private final OnlineUserService onlineUserService;

    /**
     * 用户上线
     */
    @PostMapping("/online")
    public R<String> enter(HttpServletRequest request) {
        return R.ok(onlineUserService.online(request));
    }

    /**
     * 用户下线
     */
    @GetMapping("/leave")
    public R<Void> leave(@RequestParam String sessionId) {
        onlineUserService.offline(sessionId);
        return R.ok();
    }

    /**
     * 获取在线人数统计
     */
    @GetMapping("/count")
    public R<Long> getCount() {
        return R.ok(onlineUserService.getOnlineIpCount());
    }
}
